﻿
using CMS.Controls.TabPageTemplate;
using CMS.Infos;
using CMS.REST;
using Newtonsoft.Json.Linq;
using System;
using System.Windows.Forms;

namespace CMS.Controls.DisplayTabPages.SettingTabPages
{
    public class AssistantEditorTabAdapter : EditorAdaptor
    {
        public override Type ElementType => typeof(Assistant);

        public override string ItemName => "";

        public override string API => "apis/v1/assistants";

        public override string PkField => "ID";

        public override string NameField => "Name";

        public override bool AllowIndexItem => Permission.CheckPermission(Permission.Assistant.Index);

        public override bool AllowCreateItem => Permission.CheckPermission(Permission.Assistant.Save);

        public override bool AllowUpdateItem => Permission.CheckPermission(Permission.Assistant.Update);

        public override bool AllowDeleteItem => Permission.CheckPermission(Permission.Assistant.Delete);

        private FormPanel FP_Main;
        private TextBox TB_Name;
        private TextBox TB_CreditID;
        private TextBox TB_ClassName;
        private TextBox TB_College;
        private TextBox TB_Phone;
        private Button Bn_UnbindWechat;


        public override void InitializeEditor(Panel editorPanel, BindingSource source)
        {
            FP_Main = new FormPanel();
            _SettingFormPanel(editorPanel, FP_Main);
            FP_Main.SuspendLayout();

            TB_Name = new MyTextBox();
            TB_Name.DataBindings.Add("Text", source, "Name");
            FP_Main.AddField("助理名称", TB_Name, FormPanel.TextNoEmptyValidator);

            TB_CreditID = new MyTextBox();
            TB_CreditID.DataBindings.Add("Text", source, "CreditID");
            FP_Main.AddField("助理学号", TB_CreditID, FormPanel.TextNoEmptyValidator);

            TB_ClassName = new MyTextBox();
            TB_ClassName.DataBindings.Add("Text", source, "ClassName");
            FP_Main.AddField("年级班级", TB_ClassName, FormPanel.TextNoEmptyValidator);

            TB_College = new MyTextBox();
            TB_College.DataBindings.Add("Text", source, "College");
            FP_Main.AddField("所属学院", TB_College, FormPanel.TextNoEmptyValidator);

            TB_Phone = new MyTextBox();
            TB_Phone.DataBindings.Add("Text", source, "Phone");
            FP_Main.AddField("联系电话", TB_Phone, FormPanel.TextNoEmptyValidator);

            Bn_UnbindWechat = new MyButton
            {
                Text = "解除微信绑定"
            };
            Bn_UnbindWechat.Click += (sender, e) =>
            {
                var response = RESTClient.HttpPostManaged("apis/v1/assistant_unbind", new JObject
                {
                    { "id" ,(source.DataSource as Assistant).ID}
                });
                if (response.IsSuccess)
                {
                    _SendMessage(Messages.MsgType.OK, "重置微信绑定成功");
                }
                else
                {
                    _SendMessage(Messages.MsgType.ERROR, "重置微信绑定失败");
                }
            };
            FormBase.ColumnDefinition[] columnD = FP_Main.CloneColumnDefinition(false, true, false);
            columnD[1].Padding.Top = columnD[1].Padding.Bottom = 15;
            FP_Main.AddField("解除绑定", Bn_UnbindWechat, null,columnD);

            FP_Main.ResumeLayout();
        }

        public override bool UpdateDisplayMode(Panel editorPanel, DisplayMode mode)
        {
            switch (mode)
            {
                case DisplayMode.New:
                    FP_Main.ReadOnly = false;
                    Bn_UnbindWechat.Enabled = false;
                    TB_Name.Enabled = TB_ClassName.Enabled = TB_College.Enabled = TB_CreditID.Enabled = TB_Phone.Enabled = true;
                    break;
                case DisplayMode.Edit:
                    FP_Main.ReadOnly = false;
                    Bn_UnbindWechat.Enabled = TB_Name.Enabled = TB_ClassName.Enabled = TB_College.Enabled = TB_CreditID.Enabled = TB_Phone.Enabled = true;
                    break;
                case DisplayMode.Preview:
                    FP_Main.ReadOnly = true;
                    Bn_UnbindWechat.Enabled = true;
                    TB_Name.Enabled = TB_ClassName.Enabled = TB_College.Enabled = TB_CreditID.Enabled = TB_Phone.Enabled = false;
                    break;
                case DisplayMode.None:
                    Bn_UnbindWechat.Enabled = false;
                    break;
                default:
                    return false;
            }
            return true;
        }

        public override bool ValidateForm()
        {
            return FP_Main.Validate();
        }
    }
}
